/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Configuracion.java
 *
 * Created on 14/06/2009, 03:33:02 PM
 */

package simulador;

import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
//import org.jdesktop.application.Action;

/**
 *
 * @author ea54517
 */
public class Configuracion extends javax.swing.JFrame {

    /**Referencia al objeto de interfaz gráfica, mediante la cual
     * asigna los valores de quantum y tiempo de acceso a memoria
     */
    private SimuladorView interfaz;

    public Configuracion(SimuladorView i) {
        this.interfaz = i;

        initComponents();
        this.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
        this.setResizable(false);

        this.tbQuamtum.setText(this.interfaz.getQuantum()+"");
        this.tbTiempoLectEscr.setText(this.interfaz.getTiempoAccesoMemoria()+"");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        tbQuamtum = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tbTiempoLectEscr = new javax.swing.JTextField();
        btCancelar = new javax.swing.JButton();
        btAceptar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(simulador.SimuladorApp.class).getContext().getResourceMap(Configuracion.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setAlwaysOnTop(true);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setMinimumSize(new java.awt.Dimension(289, 175));
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        tbQuamtum.setText(resourceMap.getString("tbQuamtum.text")); // NOI18N
        tbQuamtum.setName("tbQuamtum"); // NOI18N
        tbQuamtum.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbQuamtumKeyTyped(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        tbTiempoLectEscr.setText(resourceMap.getString("tbTiempoLectEscr.text")); // NOI18N
        tbTiempoLectEscr.setName("tbTiempoLectEscr"); // NOI18N
        tbTiempoLectEscr.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbTiempoLectEscrKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tbQuamtum, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                    .addComponent(tbTiempoLectEscr, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tbQuamtum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tbTiempoLectEscr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(49, 49, 49))
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(simulador.SimuladorApp.class).getContext().getActionMap(Configuracion.class, this);
        btCancelar.setAction(actionMap.get("btCancelarActionPerformed")); // NOI18N
        btCancelar.setText(resourceMap.getString("btCancelar.text")); // NOI18N
        btCancelar.setName("btCancelar"); // NOI18N
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        btAceptar.setText(resourceMap.getString("btAceptar.text")); // NOI18N
        btAceptar.setName("btAceptar"); // NOI18N
        btAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btAceptar)
                        .addGap(18, 18, 18)
                        .addComponent(btCancelar))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCancelar)
                    .addComponent(btAceptar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        this.dispose();
}//GEN-LAST:event_btCancelarActionPerformed

    private void btAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAceptarActionPerformed
        try{
            this.interfaz.setQuantum(Integer.parseInt(this.tbQuamtum.getText()));
            this.dispose();
        }
        catch(NumberFormatException e){
            javax.swing.JOptionPane.showMessageDialog(this,
                                                      "quantum incorrecto",
                                                      "Error",
                                                      javax.swing.JOptionPane.ERROR_MESSAGE);
            this.tbQuamtum.setText("");
        }
        try{
            this.interfaz.setTiempoAccesoMemoria(Integer.parseInt(this.tbTiempoLectEscr.getText()));
            this.dispose();
        }
        catch(NumberFormatException e){
            javax.swing.JOptionPane.showMessageDialog(this,
                                                      "Tiempo de acceso a memoria incorrecto",
                                                      "Error",
                                                      javax.swing.JOptionPane.ERROR_MESSAGE);
            this.tbQuamtum.setText("");
            this.tbTiempoLectEscr.setText("");
        }
        this.tbQuamtum.setText(this.interfaz.getQuantum()+"");
        this.tbTiempoLectEscr.setText(this.interfaz.getTiempoAccesoMemoria()+"");
    }//GEN-LAST:event_btAceptarActionPerformed

    private void tbQuamtumKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbQuamtumKeyTyped
        char caracter = evt.getKeyChar();

        // Verificar si la tecla pulsada no es un digito
        if(((caracter < '0') ||
        (caracter > '9')) &&
        (caracter != KeyEvent.VK_BACK_SPACE))
        {
        evt.consume();  // ignorar el evento de teclado
        }

    }//GEN-LAST:event_tbQuamtumKeyTyped

    private void tbTiempoLectEscrKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbTiempoLectEscrKeyTyped
        char caracter = evt.getKeyChar();

        // Verificar si la tecla pulsada no es un digito
        if(((caracter < '0') ||
        (caracter > '9')) &&
        (caracter != KeyEvent.VK_BACK_SPACE))
        {
        evt.consume();  // ignorar el evento de teclado
        }
    }//GEN-LAST:event_tbTiempoLectEscrKeyTyped
 

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Configuracion().setVisible(true);
            }
        });
    }*/


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAceptar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField tbQuamtum;
    private javax.swing.JTextField tbTiempoLectEscr;
    // End of variables declaration//GEN-END:variables

}
